<?php

/**
 * 挖宝用户 Controller
 * 
 * Apps/Mp/Controller/WxEventTreasureUserController.class.php
 * Zix @ 2015-08-26
 */

namespace Mp\Controller;

use Common\Service\WxEventTreasureUserService;

class WxEventTreasureUserController extends MpController {

  public function index($event_type = '', $event_id = 0) {
    $action = I('get.action', 'read');
    
    $event_obj_name = 'Common\Service\WxEvent' . $event_type . 'Service';
    $EventObj = $event_obj_name::instance();
    $event_data = $EventObj->get_by_id($event_id);

    if ($event_data['mer_id'] != $this->mer_id) {
      gameover('商户信息错误');
    }
    
    $data = $this->_init_data();
    $data['module_name'] = '挖宝用户';
    $data['module_url'] = U( $this->base_path . 'index');
    
    $data['event_name'] = $EventObj->name . ' 活动';
    $data['event_url'] = U('Mp/WxEvent' . $event_type . '/index') . '/' . $event_data['id'];
    
    $WxEventTreasureUser = WxEventTreasureUserService::instance();
    $data['config'] = array(
      'action' => $action,
      'action_label' => $this->action_label[$action],
      'menu_id' => 'menu_Mp_MerWxSetting_index',
      'event_type' => $event_type,
      'event_id' => $event_id,
      'module_url' => U($this->base_path . 'index'),
      'read_url'   => U($this->base_path . 'read' ),
      'create_url' => U($this->base_path . 'create'),
      'update_url' => U($this->base_path . 'update'),
      'delete_url' => U($this->base_path . 'delete'),
      'assist_url' => U('Mp/WxEventTreasureUserAssist/index') . '/' . $event_id . '/',
      'page' => 1,
      'page_size' => C('PAGE_SIZE'),
      'row' => $WxEventTreasureUser->get_default_row() 
    );

    $data['param'] = array();

    $this->display_with_layout($data, $this->controller .':index');
  }

  function read( $event_id ) {
    $post = I('post.');

    $config = array(
      'event_type' => $post['event_type'],
      'event_id' => $post['event_id'],
      'page' => $post['page'],
      'page_size' => $post['page_size'],
      'sort' => empty($post['sort']) ? 'id' : $post['sort'],
      'order' => empty($post['order']) ? 'DESC' : $post['order'],
    );

    $WxEventTreasureUser = WxEventTreasureUserService::instance();
    $data['rows'] = $WxEventTreasureUser->get_by_cond($config);

    $config['count'] = TRUE;
    $data['total'] = $WxEventTreasureUser->get_by_cond($config);

    $this->ajaxReturn($data);
  }

  function create() {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }
    
    $WxEventTreasureUser = WxEventTreasureUserService::instance();
    $ret = $WxEventTreasureUser->create($post);
    $this->ajaxReturn($ret);
  }

  function update($id = 0) {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }

    $WxEventTreasureUser = WxEventTreasureUserService::instance();
    $ret = $WxEventTreasureUser->update($id, $post);
    $this->ajaxReturn($ret);
  }

  function delete() {
    $ids = I('post.ids');
    $WxEventTreasureUser = WxEventTreasureUserService::instance();
    $ret = $WxEventTreasureUser->delete($ids); 
    $this->ajaxReturn($ret);
  }
}
